/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

-- Lets load this gamemode!
Loaded = false

RandRestart = "ww_"..tostring( math.random(-1000, 1000) )

function KeepTime()

	if Restarting then return false end
	
	TimeStart = math.Round(CurTime())
	
	if TimeStart == (15 * 60) then
		NotifyAll("The server will restart in 15 minutes!", 5)
	elseif TimeStart == (25 * 60) then 
		NotifyAll("The server will restart in 5 minutes!", 5)
	elseif	TimeStart == (29 * 60) then
		NotifyAll("the server will restart in 1 minute!", 5)
	end
	
	if TimeStart >= 30 * 60 then
		Restarting = true
		RunConsoleCommand("ww_restartserv")
	end
end
timer.Create("KeepTime", 1, 0, KeepTime)

function SaveMsg( msg )

	Msg(msg)
	FormattedStatus = FormattedStatus .. msg 
	
end

DeriveGamemode("sandbox");
FormattedStatus = ""

SaveMsg("=============================================================================\n"); 
SaveMsg("StargateWW Loading!\n"); 
SaveMsg("\n\n~~~~~~~~~~~ Loading Lua ~~~~~~~~~~~\n\n") 
require("datastream")

include("shared.lua")
AddCSLuaFile("shared.lua")
SaveMsg("Loaded shared.lua\n") 

WW.RandomServCall = tostring(math.random(0, 10000))

local SVfiles = file.Find("../gamemodes/StargateWW/gamemode/sv/sv_*.lua");
for k,v in pairs(SVfiles) do
	include("sv/"..v);
	SaveMsg("Loaded "..v.."\n");
end
	
local CLfiles = file.Find("../gamemodes/StargateWW/gamemode/cl/cl_*.lua");
for k,v in pairs(CLfiles) do
	AddCSLuaFile("cl/"..v);
	SaveMsg("Loaded "..v.."\n"); 
end
	AddCSLuaFile("cl_init.lua");
	AddCSLuaFile("cl_scoreboard.lua");
	SaveMsg("Loaded cl_scoreboard.lua\n"); 
	SaveMsg("Loaded cl_init.lua\n"); 

local SBfiles = file.Find("../gamemodes/StargateWW/gamemode/sb/sb_*.lua");
for k,v in pairs(SBfiles) do
	AddCSLuaFile("sb/"..v);
	SaveMsg("Loaded "..v.."\n"); 
end

local IncludeFiles = {"extentions", "flavor", "chatcommands", "upgrademanager"}
for k,v in pairs(IncludeFiles) do
	include("sh/sh_" .. v .. ".lua")
	AddCSLuaFile("sh/sh_" .. v .. ".lua")
	SaveMsg("Loaded " .. v .. "\n")
end

local Ufiles = file.Find("../gamemodes/StargateWW/gamemode/upgrades/*.lua");
for k,v in pairs(Ufiles) do
	AddCSLuaFile("upgrades/"..v);
	include("upgrades/"..v);
	SaveMsg("Loaded Upgrade "..v.."\n"); 
end

debug.AddToList()

AddDir(GM.Folder .."/content/materials/stargatedm")
AddDir(GM.Folder .."/content/materials/stargateww")
AddDir(GM.Folder .."/content/materials/wwchat/smilies")
AddDir(GM.Folder .."/content/sound/stargateww")
resource.AddFile(GM.Folder .."/content/resource/fonts/anquietas.ttf")
resource.AddFile(GM.Folder .."/content/resource/fonts/atlantis-glyphs.ttf")
resource.AddFile(GM.Folder .."/content/resource/fonts/stargate.ttf")
resource.AddFile(GM.Folder .."/content/resource/fonts/bangle_thin_normal.ttf")

SaveMsg("\n\n~~~~~~~~~~~ Loading Inits ~~~~~~~~~~~\n\n") 
InitGoalLocations()
resource.AddFile(GM.Folder .."/content/data/stargateww_changelog.txt")

SaveMsg("\n\n~~~~~~~~~~ Loading Classes ~~~~~~~~~~\n\n") 
SaveMsg("The Flavor style is " .. WW.Flavor.Name .. "\n") 
for k,v in pairs(WWClass) do
	SaveMsg( "Class " .. v.Class .. " Loaded, Team Number " .. v.Team .. " with starting argument at " .. v.ClassArg .. "\n") 
end

	function LoadWraithSpawns()
	
		WW.WraithSpawns = ents.FindByClass("info_wraith_spawn")
		
	end
	LoadWraithSpawns()
	PrintTable(WW.WraithSpawns)

SaveMsg("\n\nStargateWW Loaded!\n"); 
Loaded = true
SaveMsg("============================================================================\n"); 

if TimeStart == nil then TimeStart = 0 end

SaveMsg("===== It took " .. math.Round(TimeStart + CurTime()) .. " seconds to load the Server! ====\n") 
file.Write("StargateWW/Startup/log" .. os.date("%d") .. " - " .. os.date("%I") .. ".txt", FormattedStatus)

function GM:ShowHelp (ply)
	StartDerma(ply, "info")
end

function GM:ShowTeam(ply)
	StartDerma(ply, "team")
end

function GM:ShowSpare1(ply)
	StartDerma(ply, "help", false)
end

--################# Server caller @aVoN
local function ExecuteFunction(p,_,arg)
	local func = _G[arg[1]];
	arg[1] = p;
	func(unpack(arg));
end
concommand.Add(WW.RandomServCall,ExecuteFunction)

--################# Reload Materials @Prefanatic
function ReloadMats(p)
	Notify( p, "In 5 seconds, starting now, your game will freeze up.  THIS IS NORMAL!  It is updating the HUD Texture!", 5)

	timer.Simple( 5, function()
		game.ConsoleCommand("sv_cheats 1\n")
		p:ConCommand("mat_reloadallmaterials")
		game.ConsoleCommand("sv_cheats 0\n")
		Notify(p, "If you are unfrozen now, the reload was successful.  Thanks for playing!", 5)
		end)
end


GM.Name = "StargateWW " .. WW.Version
GM.Author = "By Prefanatic"

/*------------------------------------------------------------------------------------------------------------------
								NORMAL DOWNLOADS
------------------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------
Scope Textures
---------------------------------------------------------*/
resource.AddFile( "materials/scope/scope_normal.vmt" );
resource.AddFile( "materials/scope/scope_normal.vtf" );

resource.AddFile( "materials/scope/scope_reddot.vmt" );
resource.AddFile( "materials/scope/scope_reddot.vtf" );

/*---------------------------------------------------------
Weapon's Icons
---------------------------------------------------------*/
resource.AddFile( "materials/vgui/entities/weapon_real_cs_admin_weapon.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_ak47.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_aug.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_awp.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_desert_eagle.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_elites.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_grenade.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_famas.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_five-seven.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_flash.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_g3sg1.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_galil.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_glock18.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_knife.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_m4a1.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_m249.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_mac10.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_mp5a5.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_p90.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_p228.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_pumpshotgun.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_scout.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_sg550.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_sg552.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_smoke.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_tmp.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_ump_45.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_usp.vtf" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_xm1014.vtf" );

resource.AddFile( "materials/vgui/entities/weapon_real_cs_admin_weapon.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_ak47.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_aug.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_awp.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_desert_eagle.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_elites.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_grenade.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_famas.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_five-seven.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_flash.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_g3sg1.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_galil.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_glock18.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_knife.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_m4a1.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_m249.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_mac10.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_mp5a5.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_p90.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_p228.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_pumpshotgun.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_scout.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_sg550.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_sg552.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_smoke.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_tmp.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_ump_45.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_usp.vmt" );
resource.AddFile( "materials/vgui/entities/weapon_real_cs_xm1014.vmt" );